package com.android.kotlincoroutinemvvmdemo.contentprovider

import android.content.ContentProvider
import android.content.ContentValues
import android.content.UriMatcher
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import android.net.Uri
import android.os.Handler
import android.os.Looper
import android.util.Log
import com.android.kotlincoroutinemvvmdemo.Constants

abstract class GohiContentProvider : ContentProvider() {

    private lateinit var mSqliteHelper: GohiSqliteHelper

    protected val mUriMatcher = UriMatcher(UriMatcher.NO_MATCH)

    protected lateinit var mDBContentObserver: DBContentObserver

    override fun onCreate(): Boolean {
        Log.d(Constants.TAG, "GohiContentProvider---override fun onCreate()")
        if (this.context == null) {
            return false
        }
        //传入不同的版本号来升级数据库,只能升级,不能降级.升级会回调SQLiteOpenHelper的onUpgrade()方法
        mSqliteHelper = GohiSqliteHelper(
            this.context!!,
            SqlConstant.DB_NAME,
            null,
            SqlConstant.UPDATE_VERSION1,
            0
        ) {

        }
        //初始化内容观察者
        mDBContentObserver = DBContentObserver(this.context!!, Handler(Looper.getMainLooper()))

        onCreateOfCP()
        return true
    }

    override fun getType(uri: Uri): String = getTypeOfCP(uri)

    override fun query(
        uri: Uri,
        projection: Array<out String>?,
        selection: String?,
        selectionArgs: Array<out String>?,
        sortOrder: String?
    ): Cursor? {
        val database = mSqliteHelper.readableDatabase
        return queryOfCP(database, uri, projection, selection, selectionArgs, sortOrder)
    }

    override fun insert(uri: Uri, values: ContentValues?): Uri? {
        val database = mSqliteHelper.writableDatabase
        return insertOfCP(database, uri, values)
    }

    override fun delete(uri: Uri, selection: String?, selectionArgs: Array<out String>?): Int {
        val database = mSqliteHelper.writableDatabase
        return deleteOfCP(database, uri, selection, selectionArgs)
    }

    override fun update(
        uri: Uri,
        values: ContentValues?,
        selection: String?,
        selectionArgs: Array<out String>?
    ): Int {
        val database = mSqliteHelper.writableDatabase
        return updateOfCP(database, uri, values, selection, selectionArgs)
    }

    open fun onCreateOfCP() {}

    abstract fun getTypeOfCP(uri: Uri): String

    abstract fun queryOfCP(
        database: SQLiteDatabase,
        uri: Uri,
        projection: Array<out String>?,
        selection: String?,
        selectionArgs: Array<out String>?,
        sortOrder: String?
    ): Cursor?

    abstract fun insertOfCP(database: SQLiteDatabase, uri: Uri, values: ContentValues?): Uri?

    abstract fun deleteOfCP(
        database: SQLiteDatabase,
        uri: Uri,
        selection: String?,
        selectionArgs: Array<out String>?
    ): Int

    abstract fun updateOfCP(
        database: SQLiteDatabase,
        uri: Uri,
        values: ContentValues?,
        selection: String?,
        selectionArgs: Array<out String>?
    ): Int
}